package com.iretailer.report.web.business;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.ReHistory;
import com.iretailer.report.influxdb.model.Passengerflow;
import com.iretailer.report.model.Malls;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.StringUtil;
import com.iretailer.report.util.TimeUtil;
import org.modelmapper.TypeToken;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/4/1 13:25
 * @Description:
 */
@RestController
public class HisotryController extends BaseAction {
    @RequestMapping("/business/getHistoryFlow.action")
    public Map<String, Object> getHistoryFlow(@RequestBody ReHistory reHistory) {
        Malls malls = mallsService.getOne(new QueryWrapper<Malls>().lambda().eq(Malls::getId, reHistory.getMallId()));
        if (StringUtil.notEmpty(malls.getTimeZone())) {
            String startTime = TimeUtil.getStartTime(reHistory.getDate());
            String endTime = TimeUtil.getEndTime(reHistory.getDate());
            List zoneIds = Arrays.asList(reHistory.getCameraZone());
            List result = null;
            switch (reHistory.getZoneClass()) {
                case "Count":
                case "PassBy":
                    result = influxdbFlowService.selectFlow(malls.getTimeZone(), reHistory.getBusinessTime(), zoneIds, startTime, endTime, reHistory.getPeriod());
                    break;
                case "Dwell":
                    result = influxdbDwellService.selectDwell(malls.getTimeZone(), reHistory.getBusinessTime(), zoneIds, startTime, endTime, reHistory.getPeriod());
                    break;
            }
            return RMap.ok(result);
        } else {
            return RMap.error("not have timezone");
        }
    }

    @RequestMapping("/business/changeData.action")
    public Map<String, Object> changeData(@RequestBody ReHistory reHistory) {
        if (StringUtil.notEmpty(reHistory.getZoneClass()) && StringUtil.notEmpty(reHistory.getBusinessTime())) {
            if (reHistory.getChangeData() != null && reHistory.getChangeData().size() > 0) {
                switch (reHistory.getZoneClass()) {
                    case "Count":
                    case "PassBy":
                        List<Passengerflow> flow = JSON.parseArray(reHistory.getChangeData().toJSONString(), Passengerflow.class);
                        influxdbFlowService.upateFlow(reHistory.getBusinessTime(), flow);
                        break;
                }
            }
            return RMap.ok(true);
        } else {
            return RMap.error("zoneClass和businessTime参数的必须的");
        }
    }

    @RequestMapping("/business/copyData.action")
    public Map<String, Object> copyData(@RequestBody ReHistory reHistory) {
        String startTime = null;
        String endTime = null;
        if (StringUtil.notEmpty(reHistory.getSourceStartTime())) {
            String[] strs = reHistory.getSourceStartTime().split(":");
            startTime = TimeUtil.getDateTime(reHistory.getSourceDate(), Integer.valueOf(strs[0]), Integer.valueOf(strs[1]), 0);
        } else {
            startTime = TimeUtil.getStartTime(reHistory.getSourceDate());
        }
        if (StringUtil.notEmpty(reHistory.getSourceEndTime())) {
            String[] strs = reHistory.getSourceEndTime().split(":");
            endTime = TimeUtil.getDateTime(reHistory.getSourceDate(), Integer.valueOf(strs[0]), Integer.valueOf(strs[1]), 0);
        } else {
            endTime = TimeUtil.getEndTime(reHistory.getSourceDate());
        }
        Malls sourceMall = mallsService.getOne(new QueryWrapper<Malls>().lambda().eq(Malls::getId, reHistory.getSourceMallId()));
        Malls targetMall = mallsService.getOne(new QueryWrapper<Malls>().lambda().eq(Malls::getId, reHistory.getTargetMallId()));
        if (sourceMall.getTimeZone() != null && targetMall.getTimeZone() != null) {
            switch (reHistory.getZoneClass()) {
                case "Count":
                case "PassBy":
                    influxdbFlowService.copyFlowData(sourceMall.getTimeZone(), reHistory.getSourceCameraZoneId(), startTime, endTime, targetMall.getTimeZone(), reHistory.getTargetCameraZoneId(), reHistory.getTargetDate());
                    break;
            }
            return RMap.ok(true);
        } else {
            return RMap.error("timezone no hava");
        }
    }

}
